package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.NetworkUtils;
import com.tiantianyuegou.util.Tools;
import com.tiantianyuegou.R;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;

/**
 * 我的店铺
 */
public class ISellerActivity extends BaseActivity implements View.OnClickListener {

    private ImageView img_title;
    private TextView text_title;

    private ImageView img_seller, img_head;
    private TextView text_seller, text_shop_name, text_type, text_today_num, text_red_num, text_order_num;
    private RelativeLayout relative_today_money, relative_red_package, relative_today_order;
    private LinearLayout linear_release, linear_manage, linear_vip, linear_order, lay_isnot_vip, lay_init_vip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iseller);
        Tools.changeSystemBar(this);
        initViews();

    }

    @Override
    public void findViewByIds() {

        doShopInfo();

        text_title = (TextView) findViewById(R.id.text_title);
        img_title = (ImageView) findViewById(R.id.img_title);
        text_title.setText(R.string.seller_title);
        img_title.setOnClickListener(this);
        img_seller = (ImageView) findViewById(R.id.img_seller);
        img_head = (ImageView) findViewById(R.id.img_head);
        text_seller = (TextView) findViewById(R.id.text_seller);
        text_shop_name = (TextView) findViewById(R.id.text_shop_name);
        text_type = (TextView) findViewById(R.id.text_type);
        text_today_num = (TextView) findViewById(R.id.text_today_num);
        text_red_num = (TextView) findViewById(R.id.text_red_num);
        text_order_num = (TextView) findViewById(R.id.text_order_num);

        relative_today_money = (RelativeLayout) findViewById(R.id.relative_today_money);
        relative_red_package = (RelativeLayout) findViewById(R.id.relative_red_package);
        relative_today_order = (RelativeLayout) findViewById(R.id.relative_today_order);

        linear_release = (LinearLayout) findViewById(R.id.linear_release);
        linear_manage = (LinearLayout) findViewById(R.id.linear_manage);
        linear_vip = (LinearLayout) findViewById(R.id.linear_vip);
        linear_order = (LinearLayout) findViewById(R.id.linear_order);
        lay_isnot_vip = (LinearLayout) findViewById(R.id.lay_isnot_vip);
        lay_init_vip = (LinearLayout) findViewById(R.id.lay_init_vip);
        lay_init_vip.setVisibility(View.GONE);
    }

    LoadingDialog loadingDialog;

    private void doShopInfo() {

        if (!NetworkUtils.isNetworkAvailable(this)) {
            this.showMsg("请检查网络");
            return;
        }
        loadingDialog = new LoadingDialog(this);
        loadingDialog.showDialog();
        loadingDialog.setMsg("正在加载");
        RequestParams params = new RequestParams();
        String tmId = SPHelper.getTmId();
        params.addBodyParameter("tmId", tmId);
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.STORE_INFO, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String root = responseInfo.result;
                System.out.println(root);
                loadingDialog.dismiss();
                try {
                    JSONObject rootObj = new JSONObject(root);
                    String code = rootObj.getString("code");
                    if (code.equalsIgnoreCase("100")) {
                        String count = rootObj.getString("count");
                        JSONObject countObj = new JSONObject(count);
                        String totalCount = countObj.getString("totalCount");
                        String totalPrice = countObj.getString("totalPrice");
                        String redCount = countObj.getString("redCount");
                        String info = rootObj.getString("info");

                        if (totalPrice != "null" && !"".equals(totalPrice)) {
                            text_today_num.setText("￥" + "" +
                                    new DecimalFormat("0.00").format(Double.valueOf(totalPrice)));
                        }
                        if (redCount != "null" && !"".equals(redCount)) {
                            text_red_num.setText("" + redCount);
                        }
                        if (totalCount != "null" && !"".equals(totalCount)) {
                            text_order_num.setText("" + totalCount);
                        }

                        JSONObject infoObj = new JSONObject(info);
                        String tsId = infoObj.getString("tsId");
                        String tsName = infoObj.getString("tsName");
                        String tcId = infoObj.getString("tcId");
                        String tcName = infoObj.getString("tcName");
                        String tsLogo = infoObj.getString("tsLogo");
                        String pid = infoObj.getString("pid");
                        String province = infoObj.getString("province");
                        String cid = infoObj.getString("cid");
                        String city = infoObj.getString("city");
                        String oid = infoObj.getString("oid");
                        String county = infoObj.getString("county");
                        String tsAddress = infoObj.getString("tsAddress");
                        SPHelper.setTSId(tsId);
                        text_shop_name.setText(tsName);
                        text_type.setText("经营类别:" + tcName);
                        lay_init_vip.setVisibility(View.VISIBLE);
                        ImageLoader.getInstance().displayImage(tsLogo, img_head);

                    } else {
                        lay_isnot_vip.setVisibility(View.VISIBLE);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
//                    showMsg("解析失败");
                    lay_isnot_vip.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                loadingDialog.dismiss();
            }
        });

    }

    @Override
    public void initListener() {
        linear_manage.setOnClickListener(this);
        linear_order.setOnClickListener(this);
        linear_release.setOnClickListener(this);
        linear_vip.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_title:
                finish();
                break;

            case R.id.linear_release:
                Intent intent = new Intent(ISellerActivity.this, PublicProductActivity.class);
                startActivity(intent);
                break;
            case R.id.linear_manage:
                Intent linear_manage = new Intent(ISellerActivity.this, GoodsManageActivity.class);
                startActivity(linear_manage);
                break;

            case R.id.linear_vip:
                Intent linear_vip = new Intent(ISellerActivity.this, MyVipAvtivity.class);
                startActivity(linear_vip);

                break;

            case R.id.linear_order:
                Intent linear_order = new Intent(ISellerActivity.this, MyOrderActivity.class);
                linear_order.putExtra("type", "seller");
                startActivity(linear_order);
                break;

            default:
                break;
        }
    }
}
